/**
 * 
 */
package Modele;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import PathFinding.AbstractNode;
import PathFinding.Map;
import PathFinding.Node;
import PathFinding.NodeList;


public abstract class Fourmi implements java.io.Serializable{
	
	protected Fourmiliere home;
	
	private String name;

	private int pointDeVie;

	private int age;

	private Case location;

	private Case locPrec;
	
	private boolean moved;
	
	private List<Position> cheminASuivre;

	
	public boolean isMoved() {
		return moved;
	}

	public void setMoved(boolean moved) {
		this.moved = moved;
	}

	public Case getLocPrec() {
		return locPrec;
	}

	public void setLocPrec(Case locPrec) {
		this.locPrec = locPrec;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setPointDeVie(int pointDeVie) {
		this.pointDeVie = pointDeVie;
	}
	
	public int getPointDeVie() {
		return pointDeVie;
	}
	
	public Fourmiliere getHome() {
		return home;
	}

	public void setHome(Fourmiliere home) {
		this.home = home;
	}
	
	public int incAge()
	{
		this.age++;
		if(this.age>75)
			return 1;
		else
			return 0;
	}

	
	public int action()
	{
		return 0;
	}
	
	public int seDeplacer() {
		this.seNourrir();
		int isOK = 1;
		while(isOK==1)
		{
			Random rand = new Random();
			int  n = rand.nextInt(4) + 1;
			switch(n)
			{
				case 1:
					Position p1 = location.getMonde().getPositionFromCase(location);
					p1.setX(p1.getX()+1);
					Case newCase1 = location.getMonde().getCaseFromPosition(p1);
					if(newCase1!=null)
					{
						this.modifierLocation(newCase1);
						isOK = 0;
					}
					break;
					
				case 2:
					Position p2 = location.getMonde().getPositionFromCase(location);
					p2.setX(p2.getX()-1);
					Case newCase2 = location.getMonde().getCaseFromPosition(p2);
					if(newCase2!=null)
					{
						this.modifierLocation(newCase2);
						isOK = 0;
					}
					break;
				case 3:
					Position p3 = location.getMonde().getPositionFromCase(location);
					p3.setY(p3.getY()+1);
					Case newCase3 = location.getMonde().getCaseFromPosition(p3);
					if(newCase3!=null)
					{
						this.modifierLocation(newCase3);
						isOK = 0;
					}
					break;
				case 4:
					Position p4 = location.getMonde().getPositionFromCase(location);
					p4.setY(p4.getY()-1);
					Case newCase4 = location.getMonde().getCaseFromPosition(p4);
					if(newCase4!=null)
					{
						this.modifierLocation(newCase4);
						isOK = 0;
					}
					break;
				default:
					break;
			}
		}
		return isOK;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}


	public Case getLocation() {
		return location;
	}
	
	public void setLocation(Case loc) {
		location = loc;
	}

	public void modifierLocation(Case location) {
		this.locPrec = this.location;
		if(this.locPrec!=null)
			this.locPrec.retirerFourmi(this);
		this.location = location;
		this.location.ajouterFourmiSurCase(this);
	}

	/** 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 * @generated "UML vers Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public int deposerPheromone() {
		location.ajouterPheromone();
		return 0;
	}
	
	public int rentrerFourmiliere() {
		if((this.location==null) || (this.getHome()==null))
			return 1;
			Position maPos=null;
			Position myHome=null;
			Monde monde = this.location.getMonde();
			if(cheminASuivre== null)
			{
				Map<Node> myMap = new Map<Node>(monde.getHauteur(), monde.getLargeur(), new NodeList());
				for(int i=0;i<monde.getHauteur();i++)
				{
					for(int j=0;j<monde.getLargeur();j++)
					{
						myMap.setPenalty(i, j, monde.getCaseFromPosition(new Position(i,j)).getNiveauTerrain());
					}
				}
				maPos = monde.getPositionFromCase(this.location);
				myHome = monde.getPositionFromCase(this.getHome());
				if(maPos==null || myHome == null)
					return 1;
				List<Node> path = myMap.findPath(maPos.getX(), maPos.getY(), myHome.getX(), myHome.getY());
				
		        cheminASuivre = new ArrayList<Position>(path.size());
		        for(int iChem = 0; iChem < path.size();iChem++)
		        	cheminASuivre.add(iChem, new Position(path.get(iChem).getxPosition(), path.get(iChem).getyPosition()));
		        
			}
			if(cheminASuivre.size()>0)
				this.modifierLocation(monde.getCaseFromPosition(cheminASuivre.remove(0)));
			else
			{
				cheminASuivre=null;
				if(this instanceof Eclaireur)
				{
					((Eclaireur) this).setEtat(EtatEclaireuse.RECHERCHENOURRITURE);
				}else if(this instanceof Ouvrier)
				{
					((Ouvrier)this).setEtat(EtatOuvriere.RECHERCHEPHEROMONE);
				}else if(this instanceof Reine)
				{
					((Reine)this).setEtat(EtatReine.CREATIONFOURMIS);
				}
			}
			return 0;
	}


	public int seNourrir() {
		if(home.getStockNourriture()<1)
		{
			home.setStockNourriture(0);
			return 1;
		}
		else
			home.retirerNourriture(1);
		return 0;
	}

	public int mourrir() {
		this.getLocation().retirerFourmi(this);
		this.home.retirerFourmiFourmiliere(this);
		this.setLocation(null);
		this.setLocPrec(null);
		return 0;
	}

	public List<Position> getCheminASuivre() {
		return cheminASuivre;
	}

	public void setCheminASuivre(List<Position> cheminASuivre) {
		this.cheminASuivre = cheminASuivre;
	}
}